module TLV5618_Driver_top(
	clk			,
	rst_n			,
	// 按键
	key			,
	// 数码管
	HEX8_DIO		,	
	HEX8_SRCLK	,
	HEX8_RCLK	,
	// 拨码开关
	SW				,
	// 指示灯
	led			,
	// SPI
	DAC_cs_n		,
	DAC_sclk		,
	DAC_din	
	);

	input 			clk			;
	input 			rst_n			;
	input 			key			;
	output 			HEX8_DIO		;	
	output 			HEX8_SRCLK	;
	output 			HEX8_RCLK	;
	input [1:0] 	SW				;
	output reg 		led			;
	output 			DAC_cs_n		;
	output 			DAC_sclk		;
	output 			DAC_din		;

	reg [15:0] DAC_data;
	wire [31:0] data;
	wire key_p_flag;
	wire set_done;
	reg set_go;
	
	assign data = {2'd0,SW,12'd0,DAC_data};
	
	TLV5618_Driver TLV5618_Driver(
		.clk			(clk		),
		.rst_n		(rst_n	),
		.DAC_data	(DAC_data),
		.set_go		(set_go	),
		.set_done	(set_done),
		.DAC_cs_n	(DAC_cs_n),
		.DAC_sclk	(DAC_sclk),
		.DAC_din		(DAC_din	)
	);

	key_filter key_filter(
		.clk			(clk			),
		.rst_n		(rst_n		),
		.key			(key			),
		.key_p_flag	(key_p_flag ),
		.key_state	( 		    	),
		.key_r_flag	(			 	)
	);
    

	hex8_HC595_top hex8_HC595_top(
		.clk		(clk			), 
		.rst_n	(rst_n		),
		.data 	(data 		),
		.DIO		(HEX8_DIO	),
		.SRCLK	(HEX8_SRCLK	),
		.RCLK		(HEX8_RCLK	)
	);
	
	always@(posedge clk or negedge rst_n)
		if (!rst_n)
			set_go <= 1'b0;
		else if (key_p_flag)
			set_go <= 1'b1;
		else 
			set_go <= 1'b0;
			
			
	always@(posedge clk or negedge rst_n)
		if (!rst_n)
			DAC_data <= 16'd0;
		else begin
			case (SW)
				2'd0:DAC_data <= 16'h4200;//16'b0100_0010_0000_0000，将512写入通道B和buffer
            2'd1:DAC_data <= 16'hC400;//16'b1100_0100_0000_0000，将1024写入通道A，用buffer更新通道B
            2'd2:DAC_data <= 16'h5800;//16'b0101_1000_0000_0000，将2048写入buffer
            2'd3:DAC_data <= 16'hCFFF;//16'b1100_1111_1111_1111，将4095写入通道A，用buffer更新通道B
            default:DAC_data <= 16'h4200;
			endcase
		end

	always@(posedge clk or negedge rst_n)
		if (!rst_n)
			led <= 1'b0;
		else if (set_done)
			led <= ~led;
		
		
		
endmodule 

